package com.app114.app.android.rk;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.app114.app.android.rk.utils.UmengUtil;
import com.app114.app.android.rk.widget.SlideLayout;
import com.umeng.update.UmengDownloadListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class MainActivity extends BaseFragmentActivity implements OnNavigationListener, UmengDownloadListener,
		UmengUpdateListener {

	public static final String TAG = MainActivity.class.getSimpleName();
	private SlideLayout mSlideLayout;

	private int mCurrentPosition;
	private int mLastPosition;

	private Fragment[] mFragments;

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		setContentView(R.layout.activity_slide_ui);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.home_item,
				R.layout.sherlock_spinner_item);
		list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
		getSupportActionBar().setListNavigationCallbacks(list, this);
		mFragments = new Fragment[4];

		if (savedInstanceState == null) {
			mFragments[0] = ListFragment.newListFragment(mCurrentPosition);
			this.getSupportFragmentManager().beginTransaction()
					.add(R.id.content, mFragments[0], ListFragment.class.getSimpleName() + mCurrentPosition).commit();
		}

		mSlideLayout = (SlideLayout) findViewById(R.id.slide_layout);

		RadioGroup rg = (RadioGroup) findViewById(R.id.left_menu);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				mCurrentPosition = id2Position(checkedId);

				if (mLastPosition != mCurrentPosition) {
					mSlideLayout.switchFragment(new SwitchFragment());
				} else {
					mSlideLayout.close();
				}

			}

		});

		final int[] ids = new int[] { R.id.radio_0, R.id.radio_1, R.id.radio_2, R.id.radio_3 };
		final OnClickListener listener = new OnClickListener() {
			public void onClick(View v) {
				int position = id2Position(v.getId());
				if (position == mLastPosition) {
					mSlideLayout.close();
				}
			}
		};

		for (int i = 0; i < ids.length; i++) {
			final View v = findViewById(ids[i]);
			v.setOnClickListener(listener);
		}
	}

	private int id2Position(int checkedId) {
		switch (checkedId) {
		case R.id.radio_0:
			return 0;
		case R.id.radio_1:
			return 1;
		case R.id.radio_2:
			return 2;
		case R.id.radio_3:
			return 3;
		default:
			throw new IllegalArgumentException("invalid checked Id");
		}
	}

	private Fragment newFragment(int id) {
		Fragment fragment = null;
		switch (id) {
		case 0:
			fragment = ListFragment.newListFragment(0);
			break;
		case 1:
			fragment = new AccountFragment();
			break;
		case 2:
			fragment = new AboutFragment();
			break;
		case 3:
			fragment = new AboutFragment();
			break;
		case 4:
			fragment = new AboutFragment();
			break;

		default:
			break;
		}
		return fragment;
	}

	final class SwitchFragment implements Runnable {

		@Override
		public void run() {
			final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
			ft.hide(mFragments[mLastPosition]);
			if (mFragments[mCurrentPosition] == null) {
				mFragments[mCurrentPosition] = newFragment(mCurrentPosition);
				ft.add(R.id.content, mFragments[mCurrentPosition], ListFragment.class.getSimpleName() + mCurrentPosition);
			} else {
				ft.show(mFragments[mCurrentPosition]);
			}
			ft.commit();
			mLastPosition = mCurrentPosition;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		UmengUtil.update(mContext);
	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_slide_ui, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if (mSlideLayout.isOpen()) {
				mSlideLayout.close();
			} else {
				mSlideLayout.open();
			}
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
		return false;
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
		switch (updateStatus) {
		case 0: // has update
			UmengUpdateAgent.showUpdateDialog(mContext, updateInfo);
			break;
		case 1: // has no update
			Toast.makeText(mContext, "没有更新", Toast.LENGTH_SHORT).show();
			break;
		case 2: // none wifi
			Toast.makeText(mContext, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
			break;
		case 3: // time out
			Toast.makeText(mContext, "超时", Toast.LENGTH_SHORT).show();
			break;
		}
	}

	@Override
	public void OnDownloadEnd(int result) {
		Toast.makeText(mContext, "download result : " + result, Toast.LENGTH_SHORT).show();
	}

}
